include('shared.lua')
AddCSLuaFile( "cl_init.lua" )
AddCSLuaFile( "shared.lua" )

local ShootingSound = Sound("NPC_FloorTurret.Activate")

function ENT:Initialize()

	
	self.Enemy = nil

	self.BaseClass.Initialize( self )
	
	self.NextBigShoot = 0.0
	self.BulletPack = 0
	self.NextSmallShoot = 0.0
	self.BulletDamage = 1
	
	self.LightAttachment = self:LookupAttachment("eyes") 
	
	if self.DEBUG then Msg("Attechment: ", self.LightAttachment, "\n") end
	
end

function ENT:FireBullet()
    
	local ShootPos = self:ShootPos()
	
    local bullet = {
		Attacker = self,
		Num = 1,
		Src = ShootPos,
		Dir = ( self.Enemy:GetShootPos() - ShootPos ):Normalize(),
		Spread = Vector(0.03,0.03,0.3),
		Tracer =1,
	    Force =2,
		Damage = self.BulletDamage,
	}
   
    self:FireBullets(bullet)
end

function ENT:Think()
	
	if !self:Building() then
		if !IsValid( self.Enemy ) then
			
			for _, v in pairs( player.GetAll() ) do
			
				if (self.DEBUG || v:Team() != self:Team()) && self:CanShoot( v ) then
					
					self.Enemy = v
					self:EmitSound( ShootingSound )
					
					if self.DEBUG then Msg("Sentry: New Enemy found: ", self.Enemy, " (".. v:Team() .." | ".. self:Team() ..")\n") end
					break
				end
				
			end
			
			
		elseif !self:CanShoot( self.Enemy ) then
			self.Enemy = NULL
			if self.DEBUG then Msg("Sentry: Enemy is not shootable / Removing\n") end
		else
			
			//Shooting!
			
			if CurTime() > self.NextBigShoot then
				
				if CurTime() > self.NextSmallShoot then
					self.BulletPack = self.BulletPack + 1
					
					self:UpdateAnglesEnt( self.Enemy )
					self:FireBullet()
					
					if self.DEBUG then Msg("Sentry: Shooting!\n") end
					
					if self.BulletPack > 5 then
						self.BulletPack = 0
						self.NextBigShoot = CurTime() + 0.55
					else
						self.NextSmallShoot = CurTime() + 0.1
					end
				
				end
			end
			
		end
	end
	
	self.BaseClass.Think( self )

end

function ENT:OnRemove()
	

	util.BlastDamage( self, self, self:GetPos() + Vector(0,0,5), 12*10, 15 )
	
	self.BaseClass.OnRemove( self )

end